using System;
using System.Collections;
using System.Collections.Generic;
using System.Transactions;
using Grebok.Persistence;
using Grebok.Persistence.QueryModel;

namespace Grebok.Persistence.AdoNet
{
    using Grebok.Domain;

    public class SqlServerDataContext : BaseDataContext 
    {
        public override int GetCount<T>()
        {
            throw new NotImplementedException();
        }

        public override int GetCount<T>(Query query)
        {
            throw new NotImplementedException();
        }

        protected override IDataMapper<T> GetDataMapper<T>()
        {
            if (typeof(T) == typeof(Customer))
            {
                switch (ProviderHelper.LockMode)
                {
                    case LockMode.Nothing:
                        return (IDataMapper<T>)new CustomerDataMapper();
                    case LockMode.OptimisticOffline:
                        return (IDataMapper<T>)new OptimisticOfflineLockingCustomerDataMapper();
                    default:
                        return (IDataMapper<T>)new CustomerDataMapper();
                }
            }
            else if (typeof(T) == typeof(Employee))
            {
                return (IDataMapper<T>)new EmployeeDataMapper();
            }
            else if (typeof(T) == typeof(Product))
            {
                return (IDataMapper<T>)new ProductDataMapper();
            }
            else if (typeof(T) == typeof(Shipper))
            {
                return (IDataMapper<T>)new ShipperDataMapper();
            }
            else
            {
                throw new MissingDataProviderException("Unknown entity type");
            }
        }

        protected override void ExecuteScheduledAction(ScheduledAction action)
        {
            if (action.Target.GetType() == typeof(Customer))
            {
                Customer target = (Customer)action.Target;
                IDataMapper<Customer> dataProvider = new CustomerDataMapper();
                switch (action.Type)
                {
                    case ScheduledAction.ActionType.Create:
                        dataProvider.Create(target);
                        break;
                    case ScheduledAction.ActionType.Delete:
                        dataProvider.Delete(target);
                        break;
                    case ScheduledAction.ActionType.Update:
                        dataProvider.Update(target);
                        break;
                }
            }
            else if (action.Target.GetType() == typeof(Employee))
            {
                Employee target = (Employee)action.Target;
                IDataMapper<Employee> dataProvider = new EmployeeDataMapper();
                switch (action.Type)
                {
                    case ScheduledAction.ActionType.Create:
                        dataProvider.Create(target);
                        break;
                    case ScheduledAction.ActionType.Delete:
                        dataProvider.Delete(target);
                        break;
                    case ScheduledAction.ActionType.Update:
                        dataProvider.Update(target);
                        break;
                }
            }
        }
    }
}
